package cn.dancepeak.system.service.impl;

import cn.dancepeak.exception.BusinessException;
import cn.dancepeak.system.domain.Permission;
import cn.dancepeak.system.mapper.PermissionMapper;
import cn.dancepeak.system.query.PermissionQuery;
import cn.dancepeak.system.service.IPermissionServiceImpl;
import cn.hutool.core.util.ObjectUtil;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class PermissionServiceImplImpl implements IPermissionServiceImpl {
    @Autowired
    private PermissionMapper permissionMapper;
    @Override
    public void add(Permission permission) {
        //判断是否注册过
        Permission permission1 = permissionMapper.selectByPermissionName(permission.getName());
        if(ObjectUtil.isNotNull(permission1)){
            throw new BusinessException("用户已被注册");
        }
        //密码新增以后要加密
        permissionMapper.insert(permission);
    }

    @Override
    public void update(Permission permission) {
        //用户名不能修改   禁用  在前端写
        permissionMapper.updateById(permission);
    }

    @Override
    public void delete(Long id) {
        permissionMapper.deleteById(id);
    }

    @Override
    public void deleteByList(List<Long> ids) {
        permissionMapper.deleteByList(ids);
    }

    @Override
    public Permission getById(Long id) {
        return permissionMapper.selectById(id);
    }

    @Override
    public PageInfo<Permission> page(PermissionQuery permissionQuery) {
        // 1.下面这句代码表示开启分页，会将紧挨着这句代码的下一次数据库执行进行分页，切记一定要放在要分页的SQL语句前面执行
        // 2.执行SQL语句带条件查询，SQL语句不需要进行分页
        PageHelper.startPage(permissionQuery.getCurrentPage(), permissionQuery.getPageSize());
        List<Permission> permission = permissionMapper.list(permissionQuery);
        // 3.将查询结果转换为PageInfo对象，PageInfo对象中有前端分页所需要的各种参数
        PageInfo<Permission> pageInfo = new PageInfo<>(permission);
        // 4.响应总条数和分页信息
        return pageInfo;
    }

    @Override
    public List<Permission> getAll() {
        return permissionMapper.all();
    }
}
